//
//  SearchViewController.m
//  一键车讯
//
//  Created by Scott on 14/10/27.
//  Copyright (c) 2014年 lanou.3g.com. All rights reserved.
//

#import "SearchViewController.h"
#import "BrandDetailViewController.h"
#import "NetHandler.h"
#import "UIImageView+WebCache.h"
#import "AIMTableViewIndexBar.h"
#import "BrandCell.h"
#import "Brand.h"
@interface SearchViewController ()<UITableViewDataSource, UITableViewDelegate, AIMTableViewIndexBarDelegate>

@property (nonatomic, retain)UITableView *brandTableView;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)NSMutableArray *arraySection;
@property (nonatomic, retain)NSMutableArray *arrRow;
@property (nonatomic, retain)NSMutableDictionary *dictionary;
@property (nonatomic, retain)AIMTableViewIndexBar *indexBar;
@end

@implementation SearchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.array = [NSMutableArray array];
        self.arraySection = [NSMutableArray array];
        self.dictionary = [NSMutableDictionary dictionary];
    }
    return self;
}

// 网络接口解析
- (void)startConnection
{
    // 品牌找车接口解析
    [NetHandler networkWithURL:@"http://mrobot.pcauto.com.cn/v2/price/brands?v=4.0.0" completion:^(id result) {
        
        NSMutableArray *dicArr = [result objectForKey:@"sections"];
        [dicArr removeObjectAtIndex:0];
        self.array = dicArr;
        for (NSDictionary *dic in self.array) {
            [self.arraySection addObject:[dic objectForKey:@"index"]];
            
        }
        for (int i = 0; i < self.arraySection.count; i++) {
            self.arrRow = [NSMutableArray array];
            for (NSDictionary *brandDic in [[self.array objectAtIndex:i] objectForKey:@"brands"]) {
                Brand *brand = [[Brand alloc] initWithDictionary:brandDic];
                [self.arrRow addObject:brand];
            }
            [self.dictionary setValue:self.arrRow forKey:self.arraySection[i]];
        }
        [self.indexBar setIndexes:self.arraySection];
        [self.brandTableView reloadData];
    }];
    
}

// 加载视图
- (void)setView
{
    self.brandTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.brandTableView.dataSource = self;
    self.brandTableView.delegate = self;
    self.brandTableView.showsVerticalScrollIndicator = NO;
    self.brandTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_brandTableView];
    [_brandTableView release];
    self.brandTableView.showsVerticalScrollIndicator = NO;// 不显示滑动条
    
    self.indexBar = [[AIMTableViewIndexBar alloc] initWithFrame:CGRectMake(300, 140, 20, self.view.frame.size.height - 180)];
    [self.view addSubview:self.indexBar];
    self.indexBar.delegate = self;
    [self.indexBar release];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"品牌找车";
    [self startConnection];
    [self setView];

    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(barLeftButtonItemAction:)] autorelease];
}

//UINavigationItem方法
- (void)barLeftButtonItemAction:(UIBarButtonItem *)barButton
{
    // 返回首页
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}


- (void)dealloc
{
    [_brandTableView release];
    [_array release];
    [_arraySection release];
    [_arrRow release];
    [_dictionary release];
    [_indexBar release];
    [super dealloc];
}

#pragma mark 协议方法

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    [self.indexBar setIndexes:self.arraySection];
    return self.arraySection.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.arraySection[section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *arr = [self.dictionary objectForKey:[self.arraySection objectAtIndex:(section)]];
    return arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str2 = @"brand";
    // 自定义cell
    BrandCell *cell = [tableView dequeueReusableCellWithIdentifier:str2];
    if (cell == nil) {
        cell = [[[BrandCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str2] autorelease];
    }
    NSMutableArray *arr = [self.dictionary objectForKey:[self.arraySection objectAtIndex:(indexPath.section)]];
    Brand *brand = arr[indexPath.row];
    cell.brand = brand;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BrandDetailViewController *brandDetailVC = [[BrandDetailViewController alloc] init];
    NSMutableArray *arr1 = [self.dictionary objectForKey:[self.arraySection objectAtIndex:(indexPath.section)]];
    brandDetailVC.brand = arr1[indexPath.row];
    [self.navigationController pushViewController:brandDetailVC animated:YES];
    [brandDetailVC release];
}


#pragma mark AIMTableViewIndexBarDelegate
- (void)tableViewIndexBar:(AIMTableViewIndexBar *)indexBar didSelectSectionAtIndex:(NSInteger)index
{
    if ([self.brandTableView numberOfSections] > index && index > -1) {
        [self.brandTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
